package cn.itcast.nems.studentrecord.service.impl.studentrecord;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Assert;
import cn.itcast.ic.common.exception.v2.BizExceptionProducer;
import cn.itcast.nems.common.account.AccountHolder;
import cn.itcast.nems.common.enumeration.NemsErrorBodyEnum;
import cn.itcast.nems.manager.util.CustomBeanUtil;
import cn.itcast.nems.studentrecord.dao.entity.StudentRecord;
import cn.itcast.nems.studentrecord.dao.entity.StudentRecordDiscount;
import cn.itcast.nems.studentrecord.dao.mapper.StudentRecordDiscountMapper;
import cn.itcast.nems.studentrecord.dto.studentrecord.StudentRecordDiscountDTO;
import cn.itcast.nems.studentrecord.dto.studentrecord.StudentRecordDiscountListDTO;
import cn.itcast.nems.studentrecord.service.studentrecord.StudentRecordDiscountService;
import cn.itcast.nems.studentrecord.service.studentrecord.StudentRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 学籍优惠表 服务实现类
 * </p>
 *
 * @author liyong
 * @since 2023-11-14
 */
@Service
@RequiredArgsConstructor
public class StudentRecordDiscountServiceImpl extends ServiceImpl<StudentRecordDiscountMapper, StudentRecordDiscount> implements StudentRecordDiscountService {
    private final StudentRecordService studentRecordService ;

    @Override
    public void updateDiscounts(String studentRecordId, List<StudentRecordDiscount> studentRecordDiscounts) {
        super.getBaseMapper().deleteByStudentRecordId(studentRecordId, AccountHolder.getRequiredAccount());
        super.saveBatch(studentRecordDiscounts);
    }

    @Override
    public StudentRecordDiscountListDTO findAllDiscountByRecordId(String recordId) {
        StudentRecord studentRecord = studentRecordService.getById(recordId);
        Assert.notNull(studentRecord, BizExceptionProducer.produce(NemsErrorBodyEnum.DATA_EXIST, "学籍ID：" + recordId));
        List<StudentRecordDiscount> list = getBaseMapper().findListByRecordId(recordId);
        List<StudentRecordDiscountDTO> discountList = CollectionUtil.isNotEmpty(list) ? list.stream().map(item -> {
            StudentRecordDiscountDTO discountDTO = CustomBeanUtil.copyProperties(item, new StudentRecordDiscountDTO());
            discountDTO.setDiscountMoney(Objects.nonNull(item.getDiscountAmount()) ?
                    BigDecimal.valueOf(item.getDiscountAmount()).divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP) : null);
            return discountDTO;
        }).toList() : null;
        StudentRecordDiscountListDTO allDiscountDTO = new StudentRecordDiscountListDTO();
        allDiscountDTO.setRecordId(recordId);
        allDiscountDTO.setDiscountList(discountList);
        allDiscountDTO.setDiscountAmount(Objects.nonNull(studentRecord.getDiscountAmount()) ?
                BigDecimal.valueOf(studentRecord.getDiscountAmount()).divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP) : null);
        return allDiscountDTO;
    }
}
